package com.example.androidmaze;
import java.util.Random;
import java.util.Scanner;



public class GamblerRobotDriver implements RobotDriver {
	Scanner reader = new Scanner(System.in);
	private Robot robot;
	
	public GamblerRobotDriver(Robot r)
	{
		this.robot = r;
	}
	
	@Override
	public void setRobot(Robot r) throws UnsuitableRobotException {
		robot = r;
		float robotBattLevel = robot.getCurrentBatteryLevel();
		float robotRotationCost = robot.getEnergyForFullRotation();
		float robotMoveCost = robot.getEnergyForStepForward();
		if ((robotBattLevel == 2500) && (robotRotationCost == 4) && (robotMoveCost == 5)) {
			try {
				robot.distanceToObstacleAhead();
				//robot.distanceToObstacleBehind();
				robot.distanceToObstacleOnLeft();
				//robot.distanceToObstacleOnRight();
				robot.canSeeGoalAhead();
				//robot.canSeeGoalBehind();
				robot.canSeeGoalOnLeft();
				//robot.canSeeGoalOnRight();
			}
			catch (UnsupportedMethodException e) {
				throw new UnsuitableRobotException();
			}
		}
		
	}

	@Override
	public boolean drive2Exit() throws Exception {
		Random random = new Random();
		boolean justRotatedRight = false;
		boolean justMovedFwd = false;
		boolean canMoveLeft = false;
		boolean canMoveRight = false;
		boolean canMoveFwd = false;
		boolean canMoveBck = false;
		
		while (!robot.hasStopped() || !robot.isAtGoal()) {
			robot.printCurrentInfo();
			System.out.println(robot.getCurrentBatteryLevel());
//			System.out.println("Type input:");
//			String input = reader.next();
			if (robot.hasStopped()) {
				throw new Exception();
			}
			else if (robot.isAtGoal()) {
				return true;
			}
			
			//start algorithm
			
			if (robot.canSeeGoalOnLeft()) {
				System.out.println("can move LEFT");
				System.out.println("moving LEFT");
				robot.rotate(Constants.LEFT_ROTATE);
				robot.decreaseBatteryLevel(robot.getEnergyForRotation());
				robot.move(1, Constants.forwards);
				robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
				justMovedFwd = true;
				justRotatedRight = false;
			}
			canMoveFwd = (robot.distanceToObstacleAhead() > 0);
			if (justRotatedRight && !canMoveFwd) { //if just rotated right but couldnt move forwards
				if (canMoveFwd) {
					int moveDirection = random.nextInt(2);
					if (moveDirection == 0) { //MOVE FWD
						System.out.println("Can move FWD");
						System.out.println("moving FWD");
						System.out.println("moving: " + 1);
						robot.move(1, Constants.forwards);
						robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
						justMovedFwd = true; //can move back from here
						justRotatedRight = false;
					}
					else { //(moveDirection ==1) //ROTATE
						int rotateDirection = random.nextInt(2);
						if (rotateDirection == 0) { //Rotate RIGHT
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							justRotatedRight = true;
							justMovedFwd = false;
						}
						else {// if (rotateDirection == 1) { //Rotate RIGHT TWICE
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						}
					}
				}
				else { //cant move fwd, Rotate
					int rotateDirection = random.nextInt(2);
					if (rotateDirection == 0) { //Rotate RIGHT
						System.out.println("rotating RIGHT");
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						justRotatedRight = true;
						justMovedFwd = false;
					}
					else {// if (rotateDirection == 1) { //Rotate RIGHT TWICE
						System.out.println("rotating RIGHT");
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
					}
				}
			}
			else {
				canMoveLeft = (robot.distanceToObstacleOnLeft() > 0);
				canMoveFwd = (robot.distanceToObstacleAhead() > 0);
				int leftDistance = robot.distanceToObstacleOnLeft();
				int fwdDistance = robot.distanceToObstacleAhead();
	
				int moveDirection = random.nextInt(3);
				if (moveDirection == 0) { //MOVE FWD
					if (canMoveFwd) {
						System.out.println("Can move FWD");
						System.out.println("moving FWD");
						System.out.println("moving: " + fwdDistance);
						robot.move(1, Constants.forwards);
						robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
						justMovedFwd = true; //can move back from here
						justRotatedRight = false;
					}
					else if (canMoveLeft) { //MOVE LEFT
						System.out.println("can move LEFT");
						System.out.println("moving LEFT");
						robot.rotate(Constants.LEFT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						robot.move(1, Constants.forwards);
						robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
						justMovedFwd = true;
						justRotatedRight = false;
					}
					else { // can't move fwd or left, rotate left or right
						int rotateDirection = random.nextInt(2);
						if (rotateDirection == 0) { //Rotate RIGHT
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							justRotatedRight = true;
							justMovedFwd = false;
						}
						else {// if (rotateDirection == 1) { //Rotate RIGHT TWICE
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						}
					}
				}
				else if (moveDirection == 1) {//MOVE LEFT		
					if (canMoveLeft) {
						System.out.println("can move LEFT");
						System.out.println("moving LEFT");
						robot.rotate(Constants.LEFT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						robot.move(1, Constants.forwards);
						robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
						justMovedFwd = true;
						justRotatedRight = false;
					}
					else if (canMoveFwd) { //MOVE FWD 
						System.out.println("Can move FWD");
						System.out.println("moving FWD");
						System.out.println("moving: " + fwdDistance);
						robot.move(1, Constants.forwards);
						robot.decreaseBatteryLevel(robot.getEnergyForStepForward());
						justMovedFwd = true; //can move back from here
						justRotatedRight = false;
					}
					else { // can't move left or fwd, rotate left or right
						int rotateDirection = random.nextInt(2);
						if (rotateDirection == 0) { //Rotate RIGHT
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							justRotatedRight = true;
							justMovedFwd = false;
						}
						else {// if (rotateDirection == 1) { //Rotate RIGHT TWICE
							System.out.println("rotating RIGHT");
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
							robot.rotate(Constants.RIGHT_ROTATE);
							robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						}
					}
				}
				else { //(moveDirection == 2) //Rotate right or left
					int rotateDirection = random.nextInt(2);
					if (rotateDirection == 0) { //Rotate RIGHT
						System.out.println("rotating RIGHT");
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						justRotatedRight = true;
						justMovedFwd = false;
					}
					else {// if (rotateDirection == 1) { //Rotate RIGHT TWICE
						System.out.println("rotating RIGHT");
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
						robot.rotate(Constants.RIGHT_ROTATE);
						robot.decreaseBatteryLevel(robot.getEnergyForRotation());
					}
				}
			}
		}
		//returns false otherwise
		return false;
	}

	@Override
	public float getEnergyConsumption() {
		return (2500 - robot.getCurrentBatteryLevel());
	}

	@Override
	public int getPathLength() {
		return robot.getDistTraveled();
	}

	
}
